﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace MiniFramework {	/// <summary>    /// 定义时间类型    /// </summary>	public enum CustomEventType {		SampleEvent,	}	public class EventCenter {		/// <summary>		/// 记录所有的时间类型， Key是事件类型， Value是事件对应的委托		/// </summary>
		private static Dictionary<CustomEventType, Delegate> _eventTable = new Dictionary<CustomEventType, Delegate>();		private static void OnAddListenering(CustomEventType eventType, Delegate callBack) {			if (!_eventTable.ContainsKey(eventType)) {				_eventTable.Add(eventType, null);			}			Delegate d = _eventTable[eventType];			if (d != null && d.GetType() != callBack.GetType()) {				throw new Exception(string.Format("尝试为事件{0}添加不同的类型的委托， 当前事件所对应的委托是{1}, 要添加的委托是{2}", eventType, d.GetType(), callBack.GetType()));			}		}		/// <summary>		/// 添加监听事件 0个参数		/// </summary>		/// <param name="eventType"></param>		/// <param name="callBack"></param>
		public static void AddListener(CustomEventType eventType, Action callBack) {			OnAddListenering(eventType, callBack);			_eventTable[eventType] = (Action)_eventTable[eventType] + callBack;		}		/// <summary>		/// 添加监听事件 1个参数		/// </summary>
		public static void AddListener<T>(CustomEventType eventType, Action<T> callBack) {			OnAddListenering(eventType, callBack);			_eventTable[eventType] = (Action<T>)_eventTable[eventType] + callBack;		}		/// <summary>		/// 添加监听事件 2个参数		/// </summary>
		public static void AddListener<T1, T2>(CustomEventType eventType, Action<T1, T2> callBack) {			OnAddListenering(eventType, callBack);			_eventTable[eventType] = (Action<T1, T2>)_eventTable[eventType] + callBack;		}		/// <summary>		/// 添加监听事件 3个参数		/// </summary>
		public static void AddListener<T1, T2, T3>(CustomEventType eventType, Action<T1, T2, T3> callBack) {			OnAddListenering(eventType, callBack);			_eventTable[eventType] = (Action<T1, T2, T3>)_eventTable[eventType] + callBack;		}		/// <summary>		/// 添加监听事件 4个参数		/// </summary>
		public static void AddListener<T1, T2, T3, T4>(CustomEventType eventType, Action<T1, T2, T3, T4> callBack) {			OnAddListenering(eventType, callBack);			_eventTable[eventType] = (Action<T1, T2, T3, T4>)_eventTable[eventType] + callBack;		}		private static void OnRemoveListenering(CustomEventType eventType, Delegate callBack) {			if (!_eventTable.ContainsKey(eventType)) {				throw new Exception(string.Format("当前不存在事件{0}, 移除失败", eventType));			}			Delegate d = _eventTable[eventType];			if (d == null) {				throw new Exception(string.Format("事件{0}没有对应的委托", eventType));			} else if (d.GetType() != callBack.GetType()) {				throw new Exception(string.Format("尝试为事件{0}移除不同的类型的委托， 当前事件所对应的委托是{1}, 要移除的委托是{2}", eventType, d.GetType(), callBack.GetType()));			}		}		private static void OnRemovedListener(CustomEventType eventType) {			if (_eventTable[eventType] == null) {				_eventTable.Remove(eventType);			}		}		/// <summary>		/// 移除监听事件, 0个参数		/// </summary>		/// <param name="eventType"></param>		/// <param name="callBack"></param>
		public static void RemoveListener(CustomEventType eventType, Action callBack) {			OnRemoveListenering(eventType, callBack);			_eventTable[eventType] = (Action)_eventTable[eventType] - callBack;			OnRemovedListener(eventType);		}		/// <summary>		/// 移除监听事件, 1个参数		/// </summary>
		public static void RemoveListener<T>(CustomEventType eventType, Action<T> callBack) {			OnRemoveListenering(eventType, callBack);			_eventTable[eventType] = (Action<T>)_eventTable[eventType] - callBack;			OnRemovedListener(eventType);		}		/// <summary>		/// 移除监听事件, 2个参数		/// </summary>
		public static void RemoveListener<T1, T2>(CustomEventType eventType, Action<T1, T2> callBack) {			OnRemoveListenering(eventType, callBack);			_eventTable[eventType] = (Action<T1, T2>)_eventTable[eventType] - callBack;			OnRemovedListener(eventType);		}		/// <summary>		/// 移除监听事件, 3个参数		/// </summary>
		public static void RemoveListener<T1, T2, T3>(CustomEventType eventType, Action<T1, T2, T3> callBack) {			OnRemoveListenering(eventType, callBack);			_eventTable[eventType] = (Action<T1, T2, T3>)_eventTable[eventType] - callBack;			OnRemovedListener(eventType);		}		/// <summary>		/// 移除监听事件, 4个参数		/// </summary>
		public static void RemoveListener<T1, T2, T3, T4>(CustomEventType eventType, Action<T1, T2, T3, T4> callBack) {			OnRemoveListenering(eventType, callBack);			_eventTable[eventType] = (Action<T1, T2, T3, T4>)_eventTable[eventType] - callBack;			OnRemovedListener(eventType);		}		/// <summary>		/// 广播事件 0个参数		/// </summary>		/// <param name="eventType"></param>
		public static void BroadcastEvent(CustomEventType eventType) {			Delegate d;			if (_eventTable.TryGetValue(eventType, out d)) {				Action callback = d as Action;				if (callback == null) {					throw new Exception(string.Format("广播事件错误，事件{0}具有不同的类型"));				} else {					callback();				}			}		}		/// <summary>		/// 广播事件 1个参数		/// </summary>		/// <param name="eventType"></param>
		public static void BroadcastEvent<T>(CustomEventType eventType, T arg) {			Delegate d;			if (_eventTable.TryGetValue(eventType, out d)) {				Action<T> callback = d as Action<T>;				if (callback == null) {					throw new Exception(string.Format("广播事件错误，事件{0}具有不同的类型"));				} else {					callback(arg);				}			}		}		/// <summary>		/// 广播事件 2个参数		/// </summary>		/// <param name="eventType"></param>
		public static void BroadcastEvent<T1, T2>(CustomEventType eventType, T1 arg1, T2 arg2) {			Delegate d;			if (_eventTable.TryGetValue(eventType, out d)) {				Action<T1, T2> callback = d as Action<T1, T2>;				if (callback == null) {					throw new Exception(string.Format("广播事件错误，事件{0}具有不同的类型"));				} else {					callback(arg1, arg2);				}			}		}		/// <summary>		/// 广播事件 3个参数		/// </summary>		/// <param name="eventType"></param>
		public static void BroadcastEvent<T1, T2, T3>(CustomEventType eventType, T1 arg1, T2 arg2, T3 arg3) {			Delegate d;			if (_eventTable.TryGetValue(eventType, out d)) {				Action<T1, T2, T3> callback = d as Action<T1, T2, T3>;				if (callback == null) {					throw new Exception(string.Format("广播事件错误，事件{0}具有不同的类型"));				} else {					callback(arg1, arg2, arg3);				}			}		}		/// <summary>		/// 广播事件 4个参数		/// </summary>		/// <param name="eventType"></param>
		public static void BroadcastEvent<T1, T2, T3, T4>(CustomEventType eventType, T1 arg1, T2 arg2, T3 arg3, T4 arg4) {			Delegate d;			if (_eventTable.TryGetValue(eventType, out d)) {				Action<T1, T2, T3, T4> callback = d as Action<T1, T2, T3, T4>;				if (callback == null) {					throw new Exception(string.Format("广播事件错误，事件{0}具有不同的类型"));				} else {					callback(arg1, arg2, arg3, arg4);				}			}		}	}}
